<?php
/**
 * Created by PhpStorm.
 * User: zoupeng
 * Date: 2018/9/8
 * Time: 上午1:08
 */

namespace app\admin\controller;

use app\admin\model\Category as CategoryModel;
use app\admin\model\Goods as GoodsModel;
use app\admin\model\ShoppingGuideCollect as ShoppingGuideCollectModel;

class ShoppingGuideCollect extends Common
{
    /**
     * @Notes:   首页数据展示
     * @User:    this邹
     * @CreateDate:  2019/2/27 0027 17:18
     * @return array|mixed|\think\response\View
     */
    public function index()
    {
        $SGCModel = new ShoppingGuideCollectModel();
        $shoppingGuideList = $SGCModel->findShoppingGuideList();
        $this->assign('shoppingGuideList', $shoppingGuideList);
        $today = strtotime(date('Y-m-d', time()));
        $this->assign('Today', $today);
        return view();
    }

    /**
     * 添加
     * @return array|mixed|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            // 页数大小
            if ($data['page_size'] > 100) {
                $data['page_size'] = 100;
            } else if ($data['page_size'] < 1) {
                $data['page_size'] = 1;
            }
            //页数
            if ($data['page_no'] < 1) {
                $data['page_no'] = 1;
            }
            if (ShoppingGuideCollectModel::add($data)) {
                return json(array('code' => 200, 'msg' => '添加成功'));
            } else {
                return json(array('code' => 0, 'msg' => '添加失败'));
            }
        }
        $CategoryList = CategoryModel::name('category')->select();
        $this->assign('CategoryList', $CategoryList);
        return view();
    }

    /**
     * 修改数据
     * @return array|mixed|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $SGModel = new ShoppingGuideCollectModel();

        if (request()->isPost()) {
            $data = input('post.');
            // 页数大小
            if ($data['page_size'] > 100) {
                $data['page_size'] = 100;
            } else if ($data['page_size'] < 1) {
                $data['page_size'] = 1;
            }
            //页数
            if ($data['page_no'] < 1) {
                $data['page_no'] = 1;
            }

            if ($SGModel::editShoppingGuide($data)) {
                return json(array('code' => 200, 'msg' => '修改成功'));
            } else {
                return json(array('code' => 0, 'msg' => '修改失败'));
            }
        }
        $shoppingGuideItem = $SGModel->getShoppingGuideById(input('id'));
        $this->assign('shoppingGuideItem',$shoppingGuideItem);
        $this->assign('CategoryList', CategoryModel::select());
        return view();
    }

    /**
     * 删除数据--单个
     * @return \think\response\Json
     */
    public function dels()
    {
        if (ShoppingGuideCollectModel::del(input('id'))) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }

    /**
     * 删除数据--批量
     * @return \think\response\Json
     */
    public function delss()
    {
        $params = input('post.');
        $ids = implode(',', $params['ids']);
        if (ShoppingGuideCollectModel::delss($ids)) {
            return json(array('code' => 200, 'msg' => '批量删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '批量删除失败'));
        }
    }

    public function test()
    {
        $data = $this->getCoupon('564904276507', '6fed52c90fbf4e2b8a9f1b409a2d650d');
        dump($data['coupon_amount']);
    }

    /**
     * 批量分类数据采集
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collect_all()
    {
        // 查找小于当天时间的分类数据进行采集
        $nowTime = date('Y-m-d', time());
        $quanwangList = ShoppingGuideCollectModel::whereTime('lastTime', '<', $nowTime)->select();

        if (empty($quanwangList)) {
            return json(array('code' => 202, 'msg' => '当天采集已完成，明天继续采集'));
        }
        // 采集总页数量
        $page_no = 0;
        // 采集总商品数量
        $num = 0;
        foreach ($quanwangList as $item) {
            $page_no += $item['page_no'];
            $res = $this->collect_unit($item);
            if ($res['code'] == 202) {
                return json($res);
            } else {
                // 累加采集商品总数
                $num += $res['num'];
                //更新采集信息
                ShoppingGuideCollectModel::where(['id' => $item['id']])->update(['lastPage' => $item['page_no'], 'lastTime' => time()]);
            }
        }
        return json(array('code' => 200, 'msg' => '共采集' . ($page_no) . '页,目前共采集到商品' . $num . '件。', 'num' => $num));
    }

    /**
     * 单个分类数据采集
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collect_one()
    {
        //获取采集信息
        $data = input();
        $sgcId = $data['id'];
        $SGCModel = new ShoppingGuideCollectModel();
        $shoppingGuide = $SGCModel->getShoppingGuideById($sgcId);
        if(!$shoppingGuide){
            return json(array('code' => 100, 'msg' => '没有这个采集分类信息'));
        }

        // 此数组用于修改ShoppingGuideCollect数据用
        $SGCItem = array();
        $SGCItem['id'] = $shoppingGuide['id'];

        if (date('Y-m-d', $shoppingGuide['lastTime']) == date('Y-m-d', time())) {
            $SGCItem['lastPage'] = 0;
            $SGCModel::editShoppingGuide($SGCItem);
            return json(array('code' => 300, 'msg' => '当天采集已完成，请在商品管理中查看！'));
        } else {
            //没完成，开始采集
            $res = $this->dgMaterialOptional($shoppingGuide);

            if ($res['code'] == 200) {
                //更新采集信息
                $SGCItem['lastPage'] = $shoppingGuide['page_no'];
                $SGCItem['lastTime'] = time();
                $SGCModel::editShoppingGuide($SGCItem);
                return json(array('code' => 200, 'msg' => '已采集' . ($shoppingGuide['page_no']) . '页,目前共采集到商品' . $res['num'] . '件!'));
            } else {
                return json(array('code' => 400, 'msg' => '采集数据异常!'));
            }
        }
    }
}